package com.hung.view.page;

import com.hung.pojo.Part;
import com.hung.service.serviceImpl.PartServiceImpl;
import com.hung.util.ListToVector;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;

/**
 * 管理瓜圈页面
 *
 * @author Hung
 */
public class PartManageComponent extends Box {

    private JTable table;
    private Vector<String> title = new Vector<>();
    private Vector<Vector> data = new Vector<>();
    private DefaultTableModel tableModel;

    public PartManageComponent() {
        super(BoxLayout.Y_AXIS);
        String[] titles = {"瓜圈id", "瓜圈名字", "创建时间", "管理"};

        List<Part> parts = new PartServiceImpl().queryAllParts();
        Vector<Vector> vectors = ListToVector.pListToVector(parts);
        data.clear();
        for (Vector vector : vectors) {
            data.add(vector);
        }

        for (String t : titles) {
            title.add(t);
        }

        //初始化页面
        tableModel = new DefaultTableModel(data, title);
        //设置table
        table = new JTable(tableModel);
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        table.getColumnModel().getColumn(3).setCellEditor(new MyRender());
        table.getColumnModel().getColumn(3).setCellRenderer(new MyRender());
        this.add(new JScrollPane(table));
    }

    /**
     * 渲染器 编辑器
     */
    class MyRender extends AbstractCellEditor implements TableCellRenderer, ActionListener, TableCellEditor {

        private static final long serialVersionUID = 1L;
        private JButton button = null;

        public MyRender() {
            button = new JButton("删除");
            button.addActionListener(this);
        }

        @Override
        public Object getCellEditorValue() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value,
                                                       boolean isSelected, boolean hasFocus, int row, int column) {
            // TODO Auto-generated method stub
            return button;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            int i = JOptionPane.showConfirmDialog(null, "你真的要删除改圈吗？删除了不可恢复哦");
            if (i == JOptionPane.YES_OPTION) {
                //获取选中的行的mid删除
                String pid = (String) table.getValueAt(table.getSelectedRow(), 0);
                boolean b = new PartServiceImpl().deletePartById(Integer.parseInt(pid));
                if (b) {
                    JOptionPane.showMessageDialog(null, "删除成功!");
                } else {
                    JOptionPane.showMessageDialog(null, "删除失败噢,稍后再试");
                }
            }
        }

        @Override
        public Component getTableCellEditorComponent(JTable table, Object value,
                                                     boolean isSelected, int row, int column) {
            // TODO Auto-generated method stub
            return button;
        }

    }
}
